package com.nhatviet.webservice.activities.foursquare;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Vector;
import com.nhatviet.webservice.AppMain;
import com.nhatviet.webservice.helpers.FourSquareVenueService;
import com.nhatviet.webservice.helpers.JSONVenueParse;
import com.nhatviet.webservice.helpers.OpenHttpConnection;
import com.nhatviet.webservice.models.Venue;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class FourSquareSearchActivity extends Activity
{
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.flickr_search);

		// String urlStr = FourSquareVenueService.getURL("venues", "search",
		// "40.7", "-74");
		String urlStr = FourSquareVenueService.getURL("venues", "search", "10", "106");
		Log.d("Debug", urlStr);
		// String urlStr =
		// " https://api.foursquare.com/v2/venues/search?ll=40.7,-74&oauth_token=KCLSSXVHBEEBTPKLK4MOAYC1RRBXADV3ST4CAG0SLOXNND3U&v=20120401";
		try
		{
			/* Get the stream */
			InputStream inputStream = new OpenHttpConnection().get(urlStr);
			if (inputStream == null)
			{
				AppMain.alert(FourSquareSearchActivity.this, "Network error!", "Please check the network!");
			}
			else
			{
				InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
				/* Get the string return */
				int charRead = 0;
				String resultStr = "";
				char[] charBuffer = new char[2000];
				while ((charRead = inputStreamReader.read(charBuffer)) > 0)
				{
					resultStr += String.valueOf(charBuffer, 0, charRead);
					charBuffer = new char[2000];
				}
				Log.d("Debug 1111111", resultStr);

				JSONVenueParse jsonVenueParse = new JSONVenueParse(resultStr);
				Vector<Venue> venues = jsonVenueParse.execute();
				for (int i = 0; i < venues.size(); i++)
				{
					Log.d("debug", venues.get(i).getName());
				}
			}
		} catch (Exception e)
		{
			e.printStackTrace();
		}

	}
}
